﻿using System;
using System.Web.UI.WebControls;
using EntitiesTM;
using Business;
using System.Data;

namespace TimeWork.webmaster.manager
{
    public partial class UserModified : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["mail"] == null)
            {
                Response.Redirect("http://" + Request.Url.Authority + "/webmaster/SignIn.aspx");
            }
            if (!IsPostBack)
            {
                pnlNotice.Visible = false;
                LoadContent();
            }
            
        }

        public void LoadContent()
        {
            if (Request.QueryString["email"] != null && Request.QueryString["cid"] != null)
            {
                try
                {
                    string email = Utilities.UtiGeneralClass.DecodeBase64ToString(Request.QueryString["email"].ToString());
                    lblEmail.Text = email;
                    string companyid = Utilities.UtiGeneralClass.DecodeBase64ToString(Request.QueryString["cid"].ToString());
                    EnStaff staff = BuStaff.selStaffByEmail(email, companyid);
                    txtName.Text = staff.staffName;
                    txtAddress.Text = staff.staffAddress;
                    if (staff.staffBirthday.ToString("dd/MM/yyyy").Equals("01/01/1754"))
                    {
                        txtBirthday.Text = "";
                    }
                    else
                    {
                        txtBirthday.Text = staff.staffBirthday.ToString("dd/MM/yyyy");
                    }
                    txtLevel.Text = staff.staffLevel.ToString();
                    txtPhone.Text = staff.staffPhone;
                    txtScale.Text = staff.staffScale;
                    txtPosition.Text = staff.staffPosition;

                    ddlGender.DataBind();
                    ddlGender.SelectedValue = staff.staffGender.ToString();

                    ddlPermission.DataSource = BuPermission.selAllPermission(companyid);
                    ddlPermission.DataTextField = "PermissionName";
                    ddlPermission.DataValueField = "PermissionID";
                    ddlPermission.DataBind();
                    ddlPermission.SelectedValue = staff.staffPermissionID.ToString();

                    DataTable dataResultUnit = BuResultUnit.selAllResultUnit(companyid);
                    if (dataResultUnit.Rows.Count != 0)
                    {
                        ddlResultUnit.DataSource = dataResultUnit;
                        ddlResultUnit.DataTextField = "ResultUnitName";
                        ddlResultUnit.DataValueField = "ResultUnitID";
                        ddlResultUnit.DataBind();
                        if (staff.staffResultUnitID == 0)
                        {
                            ddlResultUnit.Items.Insert(0, new ListItem("--- Chọn đơn vị ---", "0"));
                            ddlResultUnit.SelectedValue = "0";
                        }
                        else
                        {
                            ddlResultUnit.SelectedValue = staff.staffResultUnitID.ToString();
                        }
                    }
                    else
                    {
                        ddlResultUnit.DataBind();
                        ddlResultUnit.Items.Insert(0, new ListItem("--- Chọn đơn vị ---", "0"));
                        ddlResultUnit.SelectedValue = "0";
                    }

                }
                catch
                {
                    pnlChangeProfile.Visible = false;
                    lblError.Text = "Không tồn tại Email này!";
                }
            }
            else
            {
                Response.Redirect("UserManager.aspx");
            }
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            pnlNotice.Visible = true;
            string name = txtName.Text.Trim();
            txtName.CssClass = "error";
            if (!name.Equals(""))
            {
                try
                {

                    EnStaff staff = new EnStaff();
                    txtName.CssClass = "valid";
                    staff.staffEmail = lblEmail.Text;
                    staff.staffName = name;
                    staff.staffResultUnitID = Int32.Parse(ddlResultUnit.SelectedValue);
                    try
                    {
                        staff.staffBirthday = DateTime.ParseExact(txtBirthday.Text, "dd/MM/yyyy", null);
                    }
                    catch
                    {
                        staff.staffBirthday = DateTime.Parse("1/1/1754");
                    }
                    staff.staffGender = Boolean.Parse(ddlGender.SelectedValue);
                    staff.staffPhone = txtPhone.Text.Trim();
                    staff.staffAddress = txtAddress.Text.Trim();
                    staff.staffPosition = txtPosition.Text.Trim();
                    staff.staffScale = txtScale.Text.Trim();
                    staff.staffLevel = Int32.Parse(txtLevel.Text);
                    staff.staffPermissionID = Int32.Parse(ddlPermission.SelectedValue);
                    string companyid = Utilities.UtiGeneralClass.DecodeBase64ToString(Request.QueryString["cid"].ToString());
                    staff.staffCompanyID = Int32.Parse(companyid);

                    BuStaff.updStaff(staff,companyid);
                    LoadContent();
                    imgNotice.ImageUrl = "~/resources/img/complete.png";
                    lblNotice.Text = "Cập nhật thành công";
                    lblNotice.CssClass = "";

                }
                catch
                {
                    imgNotice.ImageUrl = "~/resources/img/fail.png";
                    lblNotice.Text = "Cập nhật thất bại";
                    lblNotice.CssClass = "fail";
                }
            }
            else
            {
                imgNotice.ImageUrl = "~/resources/img/fail.png";
                lblNotice.Text = "Cập nhật thất bại";
                lblNotice.CssClass = "fail";
            }
        }

        protected void btnDelte_Click(object sender, EventArgs e)
        {

        }

        protected void btnResetPass_Click(object sender, EventArgs e)
        {
            EnStaff staff = new EnStaff();
            staff.staffEmail = Utilities.UtiGeneralClass.DecodeBase64ToString(Request.QueryString["email"].ToString());
            staff.staffPassword = Utilities.UtiGeneralClass.Md5Encode("123456");
            BuStaff.resetPass(staff, Utilities.UtiGeneralClass.DecodeBase64ToString(Request.QueryString["cid"].ToString()));
            
        }
    }
}